<?php
/**
 * 商品模型
 *
 * PHP version 5
 *
 * @package    Ecga
 * @subpackage Models
 * @author     Garbin <garbing@gmail.com>
 * @copyright  2010 Xingchangxinda Inc.
 * @version    SVN: $id$
 */

namespace Project\Models\Mapper\Ecga;

use Project\Apps\Ecga\Library\Visitor;

use \Framework\Mapper\Models;
use Project\Apps\Ecga\Models\UserModelException;
/**
 * Products models
 *
 * @package    Ecga
 * @subpackage Models
 * @author     Garbin <garbing@gmail.com>
 * @copyright  2010 Xingchangxinda Inc.
 * @version    Release: 0.1
 */
class Users extends Models
{
    /**
     * set Products fields
     *
     * @return void
     */
    protected function setFields()
    {
        $this->charField('userName', 30);
        $this->emailField('userEmail', 100);
        $this->charField('password', 32);
        $this->charField('salt', 10, array('null' => true, 'blank' => true));
        $this->positiveIntegerField('lastLoginTime', array('null' => true));
        $this->charField('lastLoginIp', 15, array('null' => true, 'blank' => true));
    }

    /**
     * 设置模型的关系
     *
     * @return void
     */
    protected function setRelations() 
    {
        //...
        $this->addListener('beforeDelete', 'beforeDeleteUser');
        $this->addListener('beforeInsert', 'beforeInsertUser');
        $this->addListener('beforeUpdate', 'beforeUpdateUser');
    }
    
    /**
     * 添加新管理员前事件
     *
     * @param Entity $user 管路用户对象
     *
     * @return void
     * @author sunfengming
     * @throws UserModelException
     */
    public function beforeInsertUser($user)
    {
    	
    }
    
    /**
     * 更新管理员前事件
     *
     * @param Entity $user 管路用户对象
     *
     * @return void
     * @author sunfengming
     * @throws UserModelException
     */
    public function beforeUpdateUser($user)
    {
    	
    }
    
    /**
     * 删除管理员前事件
     *
     * @param Entity $user 管路用户对象
     *
     * @return void
     * @author sunfengming
     * @throws UserModelException
     */
    public function beforeDeleteUser($user)
    {
    	$visitor = Visitor::getInstance();
    	if (1 == $user->id) {
    		throw new UserModelException(
				'对不起，不能删除最高级别创始人。', UserModelException::NOT_DELETE_SUPER
    		);
    	} elseif ($user->id == $visitor->id) {
    		throw new UserModelException(
				'对不起，管理员不能删除自己。', UserModelException::NOT_DELETE_SELF
    		);
    	}
    }
    
    
} // END class Products extends Models